package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XIN
 * 2023/4/20
 */
@Service
public class SpuManagerServiceimpl implements SpuManagerService {
	@Autowired
	private SpuInfoService spuInfoService;
	@Autowired
	private SpuImageService spuImageService;
	@Autowired
	private SpuPosterService spuPosterService;
	@Autowired
	private SpuSaleAttrService spuSaleAttrService;
	@Autowired
	private SpuSaleAttrValueService spuSaleAttrValueService;
	
	/**
	 * spu分页列表
	 * @param category3Id
	 * @return
	 */
	@Override
	public IPage<SpuInfo> getPageSize(IPage<SpuInfo> iPage, Long category3Id) {
		LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SpuInfo::getCategory3Id,category3Id);
		queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
		return spuInfoService.page(iPage, queryWrapper);
	}
	/**
	 * 保存spu
	 * @param spuInfo
	 * @return
	 */
	@Override
	@Transactional(rollbackFor =Exception.class)
	public void saveSpuInfo(SpuInfo spuInfo) {
		//添加spu信息表
		//TODO 获取spuid？？前端传入？数据回显？？
		spuInfoService.save(spuInfo);
		//图片地址信息
		List<SpuImage> spuImageList = spuInfo.getSpuImageList();
		if (!CollectionUtils.isEmpty(spuImageList)){
			spuImageList.stream().forEach(spuImage -> {
				spuImage.setSpuId(spuInfo.getId());
			});
			spuImageService.saveBatch(spuImageList);
		}
		//海报图片信息
		List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
		if (!CollectionUtils.isEmpty(spuPosterList)){
			spuPosterList.stream().forEach(spuPoster -> {
				spuPoster.setSpuId(spuInfo.getId());
			});
			spuPosterService.saveBatch(spuPosterList);
		}
		//销售属性
		List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
		if (!CollectionUtils.isEmpty(spuSaleAttrList)){
			spuSaleAttrList.stream().forEach(spuSaleAttr -> {
				spuSaleAttr.setSpuId(spuInfo.getId());
				List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
				if (!CollectionUtils.isEmpty(spuSaleAttrValueList)){
					spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
						spuSaleAttrValue.setSpuId(spuInfo.getId());
						spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
					});
					spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
				}
			});
			spuSaleAttrService.saveBatch(spuSaleAttrList);
		}
		
		
	}
	/**
	 * 根据spuId 查询销售属性
	 * @param spuId
	 * @return
	 */
	@Override
	public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
		SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
		List<SpuSaleAttr> spuSaleAttrs=spuSaleAttrMapper.getSpuSaleAttrList(spuId);
		return spuSaleAttrs;
	}
	/**
	 * 根据商品SpuID查询当前商品所有图片
	 * @param spuId
	 * @return
	 */
	@Override
	public List<SpuImage> spuImageList(Long spuId) {
		LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SpuImage::getSpuId, spuId);
		 return spuImageService.list(queryWrapper);
	}
	
	/**
	 * 根据spuId 获取海报数据
	 * @param spuId
	 * @return
	 */
	@Override
	public List<SpuPoster> getSpuPosterBySpuId(Long spuId) {
		LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SpuPoster::getSpuId, spuId);
		List<SpuPoster> list = spuPosterService.list(queryWrapper);
		return list;
		
	}
	/**
	 * 根据spuId,skuId 获取销售属性数据
	 * @param skuId
	 * @param spuId
	 * @return
	 */
	@Override
	public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
		SpuSaleAttrMapper baseMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
		return baseMapper.getSpuSaleAttrListCheckBySku(skuId,spuId);
	}
	/**
	 * 查询每个商品SKU 销售属性 跟 SKUID 对照关系
	 *
	 * @param spuId
	 * @return {"3732|3734":24,"3732|3735":25}
	 */
	@Override
//	@GmallCache(prefix = "skuValueIdsMap")
	public Map<String, String> getSkuValueIdsMap(Long spuId) {
		HashMap<String,String> hashMap = new HashMap<>();
		SpuSaleAttrMapper baseMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
		List<Map<String,Object>> list =  baseMapper.getSkuValueIdsMap(spuId);
		if (CollectionUtils.isEmpty(list)){
			list.stream().forEach((skuSaleAttrValuMap)->{
				String skuId = skuSaleAttrValuMap.get("sku_id").toString();
				String valueIds = skuSaleAttrValuMap.get("value_ids").toString();
				hashMap.put(skuId,valueIds);
			});
		}
		return hashMap;
	}
}








